package groups.common.gui;
import groups.common.DBDumper;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;

import javax.swing.JPanel;
import java.sql.Connection;

import org.jdesktop.application.SingleFrameApplication;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * 
 */
public class InitDB extends SingleFrameApplication {
    private JPanel topPanelCenter;
    private JButton createDB;
    private JButton initLogin;
    private JTextPane returnState;
    private Connection con;
    private String driver = "org.apache.derby.jdbc.EmbeddedDriver";

    
    public static void main(String[] args) {
        launch(InitDB.class, args);
    }
    
    @Override
    protected void startup() {
    	{
	    	getMainFrame().setSize(210, 168);
    	}
        topPanelCenter = new JPanel();
        topPanelCenter.setPreferredSize(new java.awt.Dimension(208, 107));
		{
			createDB = new JButton();
			topPanelCenter.add(createDB);
			createDB.setName("createDB");
			createDB.setPreferredSize(new java.awt.Dimension(192, 25));
			createDB.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					System.out.println("createDB.actionPerformed, event="+evt);
					actionCreateDB();
				}
			});
		}
		{
			initLogin = new JButton();
			topPanelCenter.add(initLogin);
			initLogin.setName("initLogin");
			initLogin.setPreferredSize(new java.awt.Dimension(192
					, 25));
			initLogin.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					System.out.println("initLogin.actionPerformed, event="+evt);
					if (con == null){
						returnState.setText("Crie o banco de dados primeiro!");
					}
					else{
						JFrame userLogon = new UserLogon(con);
						userLogon.setVisible(true);
						getMainFrame().setVisible(false);
					}
				}
			});
		}
		{
			returnState = new JTextPane();
			topPanelCenter.add(returnState);
			returnState.setPreferredSize(new java.awt.Dimension(192, 56));
			returnState.setName("returnState");
		}
        show(topPanelCenter);
    }
    
	private void actionCreateDB() {
		DBDumper dumper = new DBDumper(driver, "jdbc:derby:endemics;create=true", "", "");
		try {
			con = dumper.initDB();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			returnState.setText("Driver invalido!");
		} catch (SQLException e) {
			e.printStackTrace();
			returnState.setText("Erro na criacao de tabelas");
		} catch (IOException e) {
			e.printStackTrace();
			returnState.setText("Conexao mal-sucedida");
		}
		returnState.setText("Conexao estabelecida");
	}

}
